package com.yanchangapp.ui.activity.purchase.approve;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.adapter.RecordEmailAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.RecordEmailBean;
import com.yanchangapp.ui.presenter.RecordEmailAtPresenter;
import com.yanchangapp.ui.view.RecordEmailAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.YCLoadingView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
public class EmailDetailActivity extends BaseActivity<RecordEmailAtView, RecordEmailAtPresenter> implements RecordEmailAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;

    private int mID;
    private static final String ID = "id";
    private RecordEmailAdapter mRecordEmailAdapter;
    private List<RecordEmailBean> mEmailBeans = new ArrayList<>();

    public static void show(Activity activity, int id) {
        Intent intent = new Intent(activity, EmailDetailActivity.class);
        intent.putExtra(ID, id);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        if (getIntent() == null || !getIntent().hasExtra(ID)) {
            finish();
        } else {
            mID = getIntent().getIntExtra(ID, -1);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_email_detail;
    }

    @Override
    protected RecordEmailAtPresenter createPresenter() {
        return new RecordEmailAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("确认邮件");
        initAdapter();
    }

    private void setLoadingState(LoadingType loadingType) {
        switch (loadingType) {
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE) {
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (recyclerView.getVisibility() == View.VISIBLE) {
                    recyclerView.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:

                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (recyclerView.getVisibility() == View.GONE) {
                    recyclerView.setVisibility(View.VISIBLE);
                }


                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (recyclerView.getVisibility() == View.VISIBLE) {
                    recyclerView.setVisibility(View.GONE);
                }


                if (llErrorNetwork.getVisibility() == View.GONE) {
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
    }


    private void initAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecordEmailAdapter = new RecordEmailAdapter(R.layout.item_email_detail, mEmailBeans);
        recyclerView.setAdapter(mRecordEmailAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryEmailDetail(mID);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        btnNextRefresh.setOnClickListener(v -> initData());
    }

    @Override
    public void queryRecordEmailSuccess(List<RecordEmailBean> emailBeanList) {
        mEmailBeans.addAll(emailBeanList);
        mRecordEmailAdapter.notifyDataSetChanged();
        setLoadingState(LoadingType.SUCCESS);
    }

    @Override
    public void queryRecordEmailFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }
}
